package org.jflame.context.filemanager;

import java.beans.Transient;
import java.util.concurrent.TimeUnit;

/**
 * 对象存储配置类
 * 
 * @author charles.zhang
 */
public class OssManagerCfg extends FileManagerCfg {

    /**
     * 接口请求地址,区别于server,server是文件外部访问地址
     * 
     * @see #getServer()
     */
    private String accessId;
    private String accessSecret;
    private String region;
    private String bucket;
    private boolean enableHttps = true;

    /**
     * 最大连接时间,默认-1,未设置. 单位秒
     */
    private int connectionTimeout = -1;
    /**
     * 读取数据超时
     */
    private int readTimeout = -1;

    private String proxyIp;
    private int proxyPort;
    private String proxyUser;
    private String proxyPassword;

    /**
     * 用的是否自定义域名
     */
    private transient boolean isCname = false;

    public OssManagerCfg() {
    }

    public OssManagerCfg(String server, String accessId, String accessSecret, String bucket) {
        this(server, accessId, accessSecret, bucket, null);
    }

    public OssManagerCfg(String server, String accessId, String accessSecret, String bucket, String region) {
        setServer(server);
        this.accessId = accessId;
        this.accessSecret = accessSecret;
        this.bucket = bucket;
        this.region = region;
    }

    public String getAccessId() {
        return accessId;
    }

    public void setAccessId(String accessId) {
        this.accessId = accessId;
    }

    public String getAccessSecret() {
        return accessSecret;
    }

    public void setAccessSecret(String accessSecret) {
        this.accessSecret = accessSecret;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    public int connectionTimeoutInMillis() {
        return (int) TimeUnit.SECONDS.toMillis(connectionTimeout);
    }

    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    @Transient
    boolean isCname() {
        return isCname;
    }

    void setCname(boolean isCname) {
        this.isCname = isCname;
    }

    public boolean isEnableHttps() {
        return enableHttps;
    }

    public void setEnableHttps(boolean enableHttps) {
        this.enableHttps = enableHttps;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public int readTimeoutInMillis() {
        return (int) TimeUnit.SECONDS.toMillis(readTimeout);
    }

    public String getProxyIp() {
        return proxyIp;
    }

    public void setProxyIp(String proxyIp) {
        this.proxyIp = proxyIp;
    }

    public int getProxyPort() {
        return proxyPort;
    }

    public void setProxyPort(int proxyPort) {
        this.proxyPort = proxyPort;
    }

    public String getProxyUser() {
        return proxyUser;
    }

    public void setProxyUser(String proxyUser) {
        this.proxyUser = proxyUser;
    }

    public String getProxyPassword() {
        return proxyPassword;
    }

    public void setProxyPassword(String proxyPassword) {
        this.proxyPassword = proxyPassword;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("OssManagerCfg [");
        if (accessId != null) {
            builder.append("accessId=")
                    .append(accessId)
                    .append(", ");
        }
        if (accessSecret != null) {
            builder.append("accessSecret=")
                    .append(accessSecret)
                    .append(", ");
        }
        if (region != null) {
            builder.append("region=")
                    .append(region)
                    .append(", ");
        }
        if (bucket != null) {
            builder.append("bucket=")
                    .append(bucket)
                    .append(", ");
        }
        builder.append("enableHttps=")
                .append(enableHttps)
                .append(", connectionTimeout=")
                .append(connectionTimeout)
                .append(", readTimeout=")
                .append(readTimeout)
                .append(", ");
        if (proxyIp != null) {
            builder.append("proxyIp=")
                    .append(proxyIp)
                    .append(", ");
        }
        if (proxyPort != 0) {
            builder.append("proxyPort=")
                    .append(proxyPort)
                    .append(", ");
        }
        if (proxyUser != null) {
            builder.append("proxyUser=")
                    .append(proxyUser)
                    .append(", ");
        }
        if (proxyPassword != null) {
            builder.append("proxyPassword=")
                    .append(proxyPassword);
        }
        builder.append("]");
        return builder.toString();
    }

}
